package com.xthh._function._ini;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._struct.HH_T_SystemParams;

public class HH_INI {
    private static SharedPreferences mSharedPreferences;

    private static synchronized SharedPreferences getPreferneces(Context con) {
        if (mSharedPreferences == null) {
            mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(con);
        }
        return mSharedPreferences;
    }

    /**
     * 打印所有
     */
    public static void print(Context con) {
        System.out.println(getPreferneces(con).getAll());
    }

    /**
     * 清空保存在默认SharePreference下的所有数据
     */
    public static void clear(Context con) {
        getPreferneces(con).edit().clear().commit();
    }

    /**
     * 保存字符串
     *
     * @return
     */
    public static void putString(HH_T_SystemParams con, String key, String value) {
        getPreferneces(con.getCtx()).edit().putString(key, value).commit();
    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public static String getString(HH_T_SystemParams con, String key) {
        return getPreferneces(con.getCtx()).getString(key, "");

    }

    /**
     * 保存整型值
     *
     * @return
     */
    public static void putInt(HH_T_SystemParams con, String key, int value) {
        getPreferneces(con.getCtx()).edit().putInt(key, value).commit();
    }

    /**
     * 读取整型值
     *
     * @param key
     * @return
     */
    public static int getInt(HH_T_SystemParams con, String key) {
        try {
            return getPreferneces(con.getCtx()).getInt(key, 0);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 保存布尔值
     *
     * @return
     */
    public static void putBoolean(Context con, String key, Boolean value) {
        getPreferneces(con).edit().putBoolean(key, value).commit();
    }

    public static void putLong(Context con, String key, long value) {
        getPreferneces(con).edit().putLong(key, value).commit();
    }

    public static long getLong(Context con, String key) {
        try {
            return getPreferneces(con).getLong(key, 0);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * t 读取布尔值
     *
     * @param key
     * @return
     */
    public static boolean getBoolean(Context con, String key, boolean defValue) {
        try {
            return getPreferneces(con).getBoolean(key, defValue);
        } catch (Exception e) {
            return defValue;
        }
    }

    /**
     * 移除字段
     *
     * @return
     */
    public static void removeString(Context con, String key) {
        getPreferneces(con).edit().remove(key).commit();
    }

    public static HH_EnumDeviceColorType getDeviceColor(HH_T_SystemParams ctx, String key) {
        int value = getInt(ctx, key);
        switch (value){
            case 0:
                return HH_EnumDeviceColorType.BLACK_WHITE_0;
            case 1:
                return HH_EnumDeviceColorType.BLACK_WHITE_YELLOW_1;
            case 2:
                return HH_EnumDeviceColorType.BLACK_WHITE_RED_2;
            case 3:
                return HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3;
            case 4:
                return HH_EnumDeviceColorType.N_COLOR_4;
            case 5:
                return HH_EnumDeviceColorType.E6_5;
            case 6:
                return HH_EnumDeviceColorType.N_COLOR_16_6;

        }

        return HH_EnumDeviceColorType.BLACK_WHITE_0;
    }

    public static void putDeviceColor(HH_T_SystemParams ctx, String key, HH_EnumDeviceColorType deviceColorType) {
        putInt(ctx, key, deviceColorType.ordinal());
    }
}
